﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Cemit.NetFramework.Logger
{
    public enum LoggerLevel
    {
        All = 1,
        Info = 5,
        Warning = 10,
        Error = 20
    }

    /// <summary>
    /// 输出
    /// </summary>
    public static class Debug
    {
        // 重要程度：log < logInfo < logwarning < logError 
        // LanQ 2019.7.28

        /// <summary>设置输出优先级</summary>
        private static LoggerLevel Logger => PolyNetwork.Settings ? PolyNetwork.Settings.logger : LoggerLevel.All;

        public static void Log(object message, [CallerLineNumber] int sourceLine = 0)
        {
            if (Logger <= LoggerLevel.All)
                UnityEngine.Debug.Log($"{GetClassName()} (line:{sourceLine}): {message}");
        }

        public static void LogInfo(object message, [CallerLineNumber] int sourceLine = 0)
        {
            if (Logger <= LoggerLevel.Info)
                UnityEngine.Debug.Log($"{GetClassName()} (line:{sourceLine}): {message}");
        }

        public static void LogWarning(object message, [CallerLineNumber] int sourceLine = 0)
        {
            if (Logger <= LoggerLevel.Warning)
                UnityEngine.Debug.LogWarning($"{GetClassName()} (line:{sourceLine}): {message}");
        }

        public static void LogError(object message, [CallerLineNumber] int sourceLine = 0)
        {
            try
            {
                if (Logger <= LoggerLevel.Error)
                    UnityEngine.Debug.LogError($"{GetClassName()} (line:{sourceLine}): {message}");
            }
            catch (Exception) { }
        }

        private static string GetClassName()
        {
            return new StackTrace()
                .GetFrame(2)
                .GetMethod()
                .ReflectedType
                .GetTopDeclaringType()
                .Name;
        }
    }
}
